<?php
	class Cart extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			if (!isset($_SESSION)) {
			    session_start();
			    session_regenerate_id();
			}
			$this->load->helper("url");
			
		}
		public function index()
		{
			$data=array();
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_today']=$this->mdeal->list_deal_today();
			
			$data['action']="cart";
			$this->load->view("main_template", $data);
		}
		public function del_cart()
		{
			$array_cart=array();
			if(isset($_SESSION['cart']))
				$array_cart=$_SESSION['cart'];
				
			$id_deal=$_REQUEST['id_deal'];	
			
			$list=array();
			foreach ($array_cart as $value =>$giatri){
				if($giatri[0]==$id_deal){
				}else
					$list[]=array($giatri[0],$giatri[1],$giatri[2],$giatri[3],$giatri[4],$giatri[5],$giatri[6]);
			}
			$array_cart=$list;
			
			$_SESSION['cart']=$array_cart;
			echo count($array_cart);
		}
		public function changeQuantity()
		{
			$array_cart=array();
			if(isset($_SESSION['cart']))
				$array_cart=$_SESSION['cart'];
				
			$id_deal=$_REQUEST['id_deal'];	
			$quantity=$_REQUEST['quantity'];	
			
			$list=array();
			foreach ($array_cart as $value =>$giatri){
				if($giatri[0]==$id_deal){
					$giatri[1]=$quantity;
				}
				$list[]=array($giatri[0],$giatri[1],$giatri[2],$giatri[3],$giatri[4],$giatri[5],$giatri[6]);
			}
			$array_cart=$list;
			
			$_SESSION['cart']=$array_cart;
			echo count($array_cart);
		}
		public function load_cart()
		{
			$array_cart=array();
			if(isset($_SESSION['cart']))
				$array_cart=$_SESSION['cart'];
			$body='<table class="cart_info" width="100%" border="1" cellspacing="0" cellpadding="5">
		        <tr >
		          <th class="delete">Xoá</th>
		          <th class="mane_dv">Sản phẩm/Dịch vụ</th>
		          <th class="dongia">Đơn giá</th>
		          <th class="soluong">Số lượng</th>
		          <th class="thanhtien">Thành tiền</th>
		        </tr>';
		       
		        $tong=0;
		        foreach ($array_cart as $value =>$giatri){
		        	$tong+=($giatri[1]*$giatri[6]);
		       $body.=' <tr>
		          <td class="delete"><a onclick="del_cart('.$giatri[0].');" style="cursor: pointer;"><img src="'.base_url("images/btn_del_item.png").'" width="20" height="20" alt="Delete" /></a></td>
		          <td class="name"><a href="'.base_url('deal_detail?did='.$giatri[0]).'"> '.$giatri[3].'</a></td>
		          <td class="dongia">'.number_format($giatri[6]).'</td>
		          <td class="soluong">
		          												<select style="min-width:50px" onchange="changeQuantity('.$giatri[0].',this.value)">
                                                                  <option value="1"'; if($giatri[1]==1) $body.=' selected="selected"'; $body.='>1</option>
                                                                  <option value="2"'; if($giatri[1]==2) $body.=' selected="selected"'; $body.='>2</option>
                                                                  <option value="3"'; if($giatri[1]==3) $body.=' selected="selected"'; $body.='>3</option>
                                                                  <option value="4"'; if($giatri[1]==4) $body.=' selected="selected"'; $body.='>4</option>
                                                                  <option value="5"'; if($giatri[1]==5) $body.=' selected="selected"'; $body.='>5</option>
                                                                  <option value="6"'; if($giatri[1]==6) $body.=' selected="selected"'; $body.='>6</option>
                                                                  <option value="7"'; if($giatri[1]==7) $body.=' selected="selected"'; $body.='>7</option>
                                                                  <option value="8"'; if($giatri[1]==8) $body.=' selected="selected"'; $body.='>8</option>
                                                                  <option value="9"'; if($giatri[1]==9) $body.=' selected="selected"'; $body.='>9</option>
                                                                  <option value="10"'; if($giatri[1]==10) $body.=' selected="selected"'; $body.='>10</option>
                                                                </select>
		          </td>
		          <td class="thanhtien">'.number_format($giatri[1]*$giatri[6]).'</td>
		        </tr>';
		     }
		      $body.='  <tr>
		          <td colspan="4" class="tong" >Tổng cộng</td>
		          <td>'.number_format($tong).'</td>
		        </tr>
		      </table>';
				
			echo $body;
		}
	}
?>